package com.repair.order.controller;

import com.repair.api.order.request.param.OrderCommentParam;
import com.repair.api.order.request.param.OrderPayParam;
import com.repair.api.order.response.vo.OrderVO;
import com.repair.base.response.PageResponse;
import com.repair.base.result.JsonResult;
import com.repair.order.pojo.dto.OrderCreateParam;
import com.repair.order.service.OrderService;
import com.repair.order.utils.PageResultHandler;
import com.repair.web.filter.TokenFilter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 郑春雨
 * @since 2024-09-29
 */
@Api(tags = "订单模块")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /*--------------------------------- 用户 ----------------------------------------------------*/

    @ApiOperation("用户预约订单")
    @PostMapping("/createOrder")
    public JsonResult<String> createOrder(@RequestBody OrderCreateParam orderCreateParam) {

        // 这里的就是在请求中获取随机token  然后用做订单幂等号
        String identifier = TokenFilter.tokenThreadLocal.get();
        orderService.createOrder(orderCreateParam, identifier);
        return JsonResult.ok("预约成功");
    }

    @ApiOperation("用户通过订单号查询订单详情")
    @GetMapping("/getOrderInfo/{orderNum}")
    public JsonResult<OrderVO> getOrderInfo(@PathVariable("orderNum") Long orderNum) {

        OrderVO orderVO = orderService.getOrderInfoByUser(orderNum);
        return JsonResult.ok(orderVO);
    }

    @ApiOperation("用户根据不同订单状态 查询订单")
    @GetMapping("/getOrders")
    public JsonResult<List<OrderVO>> getOrders(String status, int pageSize, int currentPage) {
        PageResponse<OrderVO> res = orderService.getOrders(status, pageSize, currentPage);
        return PageResultHandler.handle(res);
    }

    @ApiOperation("用户订单支付")
    @PostMapping("/payOrder")
    public JsonResult<String> payOrder(@RequestBody OrderPayParam param, HttpServletResponse response) {

        String result = orderService.payOrder(param, response);
        return JsonResult.ok("支付成功");
    }

    @ApiOperation("支付回调")
    @PostMapping("/pay/notice")
    public String payNotify(HttpServletRequest request) throws Exception {

        orderService.notice(request);
        return "success";
    }

    @ApiOperation("用户取消订单")
    @PostMapping("/cancelOrder/{orderNum}")
    public JsonResult<String> cancelOrder(@PathVariable("orderNum") String orderNum) {

        orderService.cancelOrder(orderNum);
        return JsonResult.ok("取消成功");
    }

    @ApiOperation("用户评价订单")
    @PostMapping("/rateOrder")
    public JsonResult<String> commentOrder(@RequestBody OrderCommentParam param) {

        orderService.commentOrder(param);
        return JsonResult.ok("评价成功");
    }




    /*--------------------------------- 师傅 ----------------------------------------------------*/

    @ApiOperation("师傅查询订单详情")
    @GetMapping("/worker/getOrderInfo/{orderNum}")
    public JsonResult<OrderVO> workerGetOrderInfo(@PathVariable("orderNum") Long orderNum) {

        OrderVO orderVO = orderService.getOrderInfoByWorker(orderNum);
            return JsonResult.ok(orderVO);
    }

    @ApiOperation("师傅查询订单分页")
    @GetMapping("/worker/getOrders")
    public JsonResult<List<OrderVO>> workerGetOrders(String status,int pageSize, int currentPage) {

        PageResponse<OrderVO> res = orderService.getOrdersByWorker(status, pageSize, currentPage);
        return PageResultHandler.handle(res);
    }

    @ApiOperation("师傅查询全部抢单池")
    @GetMapping("/worker/getGrabPools")
    public JsonResult<List<OrderVO>> workerGetGrabPools(int pageSize, int currentPage) {

        PageResponse<OrderVO> res = orderService.getGrabPools(pageSize, currentPage);
        return PageResultHandler.handle(res);
    }

    @ApiOperation("师傅查询全部派单池")
    @GetMapping("/worker/getDispatchPool")
    public JsonResult<List<OrderVO>> workerGetDispatchPools(int pageSize, int currentPage) {

        PageResponse<OrderVO> res = orderService.getDispatchPools(pageSize, currentPage);
        return PageResultHandler.handle(res);
    }

    @ApiOperation("/师傅抢单")
    @PostMapping("/worker/grabOrder")
    public JsonResult<String> workerGrabOrder(Long orderNum) {

        orderService.workerGrabOrder(orderNum);
        return JsonResult.ok("抢单成功");
    }

    @ApiOperation("/师傅提交施工前")
    @PostMapping("/worker/submitWorkBefore")
    public JsonResult<String> submitWorkBefore(Long orderNum, String workBeforePhotoUrl) {

        orderService.submitWorkBefore(orderNum, workBeforePhotoUrl);
        return JsonResult.ok("提交成功");
    }

    @ApiOperation("师傅提交施工后")
    @PostMapping("/worker/submitWorkAfter")
    public JsonResult<String> submitWorkAfter(Long orderNum, String workAfterPhotoUrl, String orderPrice) {

        orderService.submitWorkAfter(orderNum, workAfterPhotoUrl, orderPrice);
        return JsonResult.ok("提交成功");
    }

    @ApiOperation("师傅确认接收派单")
    @PostMapping("/worker/acceptOrder")
    public JsonResult<String> acceptOrder(String orderNum) {

        orderService.acceptOrder(orderNum);
        return JsonResult.ok("确认成功");
    }

    @ApiOperation("师傅拒接派单")
    @PostMapping("/worker/rejectOrder")
    public JsonResult<String> rejectOrder(String orderNum) {

        orderService.rejectOrder(orderNum);
        return JsonResult.ok("拒绝成功");
    }
}
